#include "RadianEditorEngine.h"
#include "OgreRoot.h"
#include "OgreResourceGroupManager.h"
#include "RadianRenderWnd.h"
#include "RadianResourceLoadListener.h"
#include "OgreConfigFile.h"

#include <QBasicTimer>
#include <QEvent>

template<> RadianEditorEngine* Ogre::Singleton<RadianEditorEngine>::ms_Singleton = 0;
//--------------------------------------------------------------------------
RadianEditorEngine::RadianEditorEngine()
	:mIsRealtimeMode(false)
	,mRadianResLoadingListener(NULL)
{
	#ifdef __DEBUG
	mRoot = new Ogre::Root("plugins_d.cfg","Ogre.cfg","RadianDemo.log");
	#else
	mRoot = new Ogre::Root("plugins.cfg","Ogre.cfg","RadianDemo.log");
	#endif 

	mRadianResLoadingListener = new RadianResourceLoadingListener();
	Ogre::ResourceGroupManager::getSingletonPtr()->addResourceGroupListener(mRadianResLoadingListener);
}
//--------------------------------------------------------------------------
RadianEditorEngine::~RadianEditorEngine()
{

}
//--------------------------------------------------------------------------
RadianEditorEngine* RadianEditorEngine::getSingletonPtr()
{
	assert( ms_Singleton ); return ms_Singleton;
}
//--------------------------------------------------------------------------
RadianEditorEngine& RadianEditorEngine::getSingleton()
{
	return *ms_Singleton;
}
//--------------------------------------------------------------------------
void RadianEditorEngine::InitializeEngine()
{
	if(Config())
		mRoot->initialise(false,"RadianEditor");

	InitializeResources();
}
//--------------------------------------------------------------------------
void RadianEditorEngine::InitializeResources()
{
	Ogre::ConfigFile cf;
	cf.load("resources.cfg");

	try
	{
		Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();

		Ogre::String secName, typeName, archName;
		while (seci.hasMoreElements())
		{
			secName = seci.peekNextKey();
			Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
			Ogre::ConfigFile::SettingsMultiMap::iterator i;
			for (i = settings->begin(); i != settings->end(); ++i)
			{
				typeName = i->first;
				archName = i->second;

				Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
					archName, typeName, secName, false);
			}
		}

	}

	catch(const Ogre::Exception& ex)
	{
		Ogre::LogManager::getSingletonPtr()->logMessage(ex.getFullDescription());
	}
}
//--------------------------------------------------------------------------
bool RadianEditorEngine::Config()
{
	Ogre::ConfigFile cfg;
	try
	{
		cfg.load("Ogre.cfg","\t:=",false);
		Ogre::ConfigFile::SectionIterator iSection = cfg.getSectionIterator();

		while(iSection.hasMoreElements())
		{
			const Ogre::String& renderSystem = iSection.peekNextKey();
			const Ogre::ConfigFile::SettingsMultiMap& settings = *iSection.getNext();

			Ogre::RenderSystem* rs = mRoot->getRenderSystemByName(renderSystem);

			if(rs)
			{
				Ogre::ConfigFile::SettingsMultiMap::const_iterator i;
				for (i = settings.begin(); i != settings.end(); ++i)
					rs->setConfigOption(i->first, i->second);
				mRoot->setRenderSystem(rs);
			}
		}
	}

	catch(...)
	{
		if(mRoot->showConfigDialog())
		{	
			mRoot->saveConfig();
			return true;
		}
		return false;
	}

	return true;

}
//--------------------------------------------------------------------------
void RadianEditorEngine::Run()
{
	// do nothing.
}
//--------------------------------------------------------------------------
void RadianEditorEngine::TickEngine()
{
	if(mIsRealtimeMode)
	{
		Ogre::Root::getSingletonPtr()->renderOneFrame();		
	}
}	
//--------------------------------------------------------------------------
